
/**
 * Namespace    Classes.Service
 * Class        Order
 * Author       Bram Vandenbussche
 * Description  Provides service level and access level logic for Order objects
 * Version      1.0
**/

var OrderService = new function() {

    var root = this;
    
    this.Orders = null;
    this.SavedFileName = "orders.xml";
    
    
    this.LoadOrders = function() {
        this.Orders = [];
        
        var xml = LoadXmlFromFile(this.SavedFileName);
        
        if (xml != undefined) {
			air.trace("OrderService: File found! Loading file xml");
            // if there is, load the settings from it
            this.LoadOrdersFromFile(xml);
            
            // Render orders
            this.RenderOrders();
        } else {
			air.trace("OrderService: File not found! Loading from WS");
            // if there isn't, create the default settings
            this.LoadOrdersFromWS(root.SaveAndRender);
        }
    }
    
    
    this.SaveAndRender = function() {
        // save them
        root.PersistOrders();
        
        // and display them
        root.RenderOrders();
    }
    
    
    this.LoadOrdersFromFile = function(xml) {
        $.each($("order", xml), function() {
            var o = new Order();
            
            // Setting aanmaken vanuit xml
            o.Deserialize($(this));
            
            // Setting opslaan
            root.AddOrder(o);
        });
    }
    
    
    this.AddOrder = function(order) {
        this.Orders[this.Orders.length] = order;
    }
    
    
    this.LoadOrdersFromWS = function(callback) {
        air.trace("OrderService: Loading orders from webservice..");
        
        var getOrdersUrl = PrestashopService.PS_MOBILE_AUTHORIZATION.api.orders["xlink:href"] + "?display=full&sort=id_DESC" + "&limit=" + SettingsService.Settings.PS_MOBILE_ORDER_VIEW_LIMIT.Value;
        
        var getOrdersArgs = {
            url: getOrdersUrl,
            method: 'GET',
            dataType: "xml",
            async: false,
            beforeSend: function(jqXHR, settings) {            
                // Authentication
                PrestashopService.PrepareAjaxCredentials(jqXHR);
                
                // Status instellen
                SetStatus("loading");
            },
            success: function(data, textStatus, jqXHR) {                
                // Return data omvormen naar JSON
                var PS_MOBILE_ORDER_LIST = $.xml2json(data);
                
                // Response data cachen
                root.AssembleFromJson(PS_MOBILE_ORDER_LIST.orders.order)
                
                // Execute callback
                callback();
            }
        }
        
        $.ajax(getOrdersArgs);
    }
    
    
    this.AssembleFromJson = function(sourceList) {
		air.trace("OrderService: Assembling orders from json");
        for (var i = 0; i < sourceList.length; i++) {
            var o = new Order(sourceList[i].id);
            
            o.AssembleFromJson(sourceList[i]);
            
            root.AddOrder(o);
        }
		air.trace("OrderService: assembly finished");
    }
    
    
    this.PersistOrders = function() {
        air.trace("OrderService: Persisting orders to file");
        
        var xml = SerializeList(this.Orders, "orders");
        
        SaveXmlToFile(xml, this.SavedFileName);
    }
    
    
    this.RenderOrders = function() {
        
        if (this.Orders == undefined) {
            this.LoadOrders();
        } else {
        
            // Tab header aanpassen met aantal
            $("#order_count").html("(" + this.Orders.length + ")");
            
            // Tab renderen
            OrderListView.RenderView();
        }
    }
	
	
	this.ClearOrders = function() {
		root.Orders = null;
	}
	
	
	this.ClearCache = function() {
		// Clear ordersfile
		DeleteFile(root.SavedFileName);
		
		// Clear orders in memory
		root.ClearOrders();
		
		// Reload orders
		root.LoadOrders();
	}
}